package
{	
	import feathers.display.Scale3Image;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.TextFormat;
	import flash.text.engine.FontLookup;
	import flash.text.engine.FontWeight;
	
	import flashx.textLayout.container.ContainerController;
	import flashx.textLayout.elements.ParagraphElement;
	import flashx.textLayout.elements.SpanElement;
	import flashx.textLayout.elements.TextFlow;
	import flashx.textLayout.formats.TextLayoutFormat;
	
	import starling.core.Starling;
	import starling.display.Button;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.EnterFrameEvent;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.text.BitmapFont;
	import starling.text.TextField;
	import starling.textures.Texture;
	import starling.utils.Color;
	import starling.utils.formatString;
	

	public class HUD extends GameObject
	{
		private var _bunkyLife:Sprite;
		private var _pauseBtn:Button;
		private var _jumpBtn:Button;
		private var _attackBtn:Button;
		private var _pauseDialog:PauseDialog;
		private var bmpFontTF:TextField; 
		private var _counter:Number=0;

		public function HUD()
		{
			super();
			_pauseBtn = new Button(Assets.getUIScreens().getTexture("pause"));
			_jumpBtn = new Button(Assets.getUIScreens().getTexture("jump"));
			_attackBtn = new Button(Assets.getUIScreens().getTexture("attack"));
			_bunkyLife = new Sprite();
			
			for(var idx:uint; idx < Bunky.MAX_HEALTH_BUNKY; ++idx)
			{
				var img:Image = new Image(Assets.getUIScreens().getTexture("bunkyhealth"));
				img.x = (img.width*0.7) * idx; 
				_bunkyLife.addChild(img);				
			}
			_bunkyLife.pivotX = _bunkyLife.width;
			_bunkyLife.pivotY = 0;
			
			this.addChild(_bunkyLife);			
			this.addChild(_pauseBtn);
			this.addChild(_jumpBtn);
			this.addChild(_attackBtn);
		//	this.addChild(bmpFontTF);
			this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);		
			//this.addEventListener(starling.events.Event.ENTER_FRAME, onEnterFrame);	
		}
		
		private function onAddedToStage(event:Event):void
		{
			
			var bitmap:Bitmap = new Assets.font();
			var texture:Texture = Texture.fromBitmap(bitmap);
			var xml:XML = XML(new Assets.fontxml());
			TextField.registerBitmapFont(new BitmapFont(texture, xml));
			bmpFontTF = new TextField(250, 30, "Score " + "0" + _counter, "AngryBirds", 24);			
			bmpFontTF.color = Color.WHITE;
			
			bmpFontTF.x = stage.stageWidth * 0.08;
			bmpFontTF.y = stage.stageHeight * 0.005;			
			addChild(bmpFontTF);
			
			_pauseBtn.scaleX = 0.5;
			_pauseBtn.scaleY = 0.5;			
			_pauseBtn.x = Starling.current.stage.stageWidth * 0.01;
			
			_jumpBtn.pivotY = _jumpBtn.height;
			_jumpBtn.x = Starling.current.stage.stageWidth* 0.02;
			_jumpBtn.y = Starling.current.stage.stageHeight * 0.99;

			
			_attackBtn.pivotX = _attackBtn.width;
			_attackBtn.pivotY = _attackBtn.height;
			_attackBtn.x = Starling.current.stage.stageWidth* 0.98;
			_attackBtn.y = Starling.current.stage.stageHeight * 0.99;
											

			_pauseBtn.addEventListener(starling.events.TouchEvent.TOUCH, onTouched);			
			
			_jumpBtn.addEventListener(starling.events.TouchEvent.TOUCH, function(e:TouchEvent):void
			{
				var touch:Touch = e.getTouch(stage);			
				
				if(touch && touch.phase == TouchPhase.ENDED)
				{
					dispatchEvent(new PlayerActionEvent(PlayerActionEvent.JUMP_ACTION, true));						
				}
			});
			
			_attackBtn.addEventListener(starling.events.TouchEvent.TOUCH, function(e:TouchEvent):void
			{
				var touch:Touch = e.getTouch(stage);			
				
				if(touch && touch.phase == TouchPhase.ENDED)
				{
					dispatchEvent(new PlayerActionEvent(PlayerActionEvent.ATTACK_ACTION, true));
				}				
			});
						
			_bunkyLife.x = Starling.current.stage.stageWidth;
			_bunkyLife.y += Starling.current.stage.stageHeight*0.01; 
		}
		
		public function collectable_counter():void
		{			
			var ret:String = "" + ++_counter;
			bmpFontTF.text = ( ret.length < 2 )?"Score " + "0" + ret:"Score " + ret;
		}    		
		
		private function onTouched(e:TouchEvent):void
		{						
			var touch:Touch = e.getTouch(stage);			
			
			if(touch != null)
			{
				if(touch.phase == TouchPhase.ENDED)
				{				
					dispatchEvent(new GameModeChangedEvent(GameModeChangedEvent.GAME_PAUSE, true));
					
					var pauseDialog:PauseDialog = new PauseDialog()
						.setButton1Listener(function():void
						{	
							dispatchEvent(new GameModeChangedEvent(GameModeChangedEvent.GAME_LEVEL_RESTART, true));
						})
						.setButton2Listener(function():void
						{
							dispatchEvent(new GameModeChangedEvent(GameModeChangedEvent.GAME_PAUSE, true));	
							removeChild(pauseDialog);															
						})
						.setButton3Listener(function():void
						{
							dispatchEvent(new GameModeChangedEvent(GameModeChangedEvent.GAME_LEVEL_QUIT, true));
						});
					
					addChild(pauseDialog);
				}			
			}
		}

		public override function onEnterFrame(dt:Number):void
		{
			
		}
		
		public function updateBunkyHealth():void
		{
			_bunkyLife.removeChildAt(_bunkyLife.numChildren-1);
		}
		
		public override function dispose():void
		{						
			super.dispose();
			_bunkyLife.dispose();
			_bunkyLife = null;
			_pauseBtn.dispose();
			_pauseBtn = null;
		} 
	}
}